//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  NotificationResult.cs
//    
// Abstract:
//
//  This module implements the NotificationResult class. This class encapsulates 
//  the outcome of a notification attempt.
//  
//--

using System;
using System.Diagnostics;
using Microsoft.OfficeCommunicationsServer.Applications.Common;

namespace Microsoft.EnhancedReminders
{
    /// <summary>
    /// Encapsulates the outcome of a notification attempt.
    /// </summary>
    [Serializable]
    public class NotificationResult
    {
        /// <summary>
        /// Descriptor for the notification result used for logging usage data.
        /// </summary>
        public const string FormatString = "Notification result: status={0}, channel={1}, type={2}, manual={3}, joinNowType={4}";

        private NotificationId _id;
        private NotificationResultStatus _status;
        private NotificationChannelType _howContacted;
        private Context _context;
        private Type _notificationType;
        private ConferenceType _joinNowConferenceType;

        /// <summary>
        /// ctor.
        /// </summary>
        /// <param name="context">Logging context</param>
        /// <param name="id">Unique ID for this notification</param>
        /// <param name="status">Status of the notification attempt</param>
        /// <param name="howContacted">The channel used to successfully contact the recipient of the notification</param>
        public NotificationResult(Context context, NotificationId id, NotificationResultStatus status, NotificationChannelType howContacted)
        {
            _context = context;
            _id = id;
            _status = status;
            _howContacted = howContacted;
            _joinNowConferenceType = ConferenceType.None;
        }

        /// <summary>
        /// ctor.
        /// </summary>
        /// <param name="notificationItem">Logging context</param>
        /// <param name="status">Status of the notification attempt</param>
        /// <param name="howContacted">The channel used to successfully contact the recipient of the notification</param>
        public NotificationResult(NotificationItem notificationItem, NotificationResultStatus status, NotificationChannelType howContacted)
        {
            Debug.Assert(notificationItem != null, "notificationItem");

            _context = notificationItem.Context;
            _id = notificationItem.Id;
            _status = status;
            _howContacted = howContacted;

            // Determine notification type and join now conference type (if a meeting).
            _notificationType = notificationItem.GetType();
            _joinNowConferenceType = ConferenceType.None;

            CalendarNotificationItem calendarItem = notificationItem as CalendarNotificationItem;
            if (calendarItem != null && calendarItem.IsJoinNow && calendarItem.ReminderItem != null)
            {
                _joinNowConferenceType = calendarItem.ReminderItem.ConferenceType;
            }
        }

        /// <summary>
        /// Produces a string representation of this object.
        /// </summary>
        /// <returns>
        /// The string representation of this object.
        /// </returns>
        /// <remarks>
        /// This is used in logging.
        /// </remarks>
        public override string ToString()
        {
            Debug.Assert(_id != null, "_id should not be null");

            return " ID: " + _id.ToString() + " Status: " + _status.ToString() + " How contacted: " + _howContacted.ToString();
        }

        /// <summary>
        /// The logging context associated with this notification.
        /// </summary>
        public Context Context
        {
            get
            {
                return _context;
            }
        }

        /// <summary>
        /// The unique ID for the associated notification.
        /// </summary>
        public NotificationId Id
        {
            get
            {
                return _id;
            }
        }

        /// <summary>
        /// Status of the notification attempt.
        /// </summary>
        public NotificationResultStatus Status
        {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
            }
        }

        /// <summary>
        /// Channel used to successfully contact the recipient of the notification.
        /// </summary>
        public NotificationChannelType HowContacted
        {
            get
            {
                return _howContacted;
            }
            set
            {
                _howContacted = value;
            }
        }

        /// <summary>
        /// Log notification result to verbose trace logs.
        /// </summary>
        public void LogTraceInfo()
        {
            Logger.WriteTraceVerbose(Context,
                                     FormatString,
                                     Status,
                                     HowContacted,
                                     _notificationType == null ? String.Empty : _notificationType.ToString(),
                                     false, // Hardcoded to log reminder was automatically generated (i.e. rules based).  This would need to be updated with a value from the scheduler to distinguish between manual and automated reminders.
                                     _joinNowConferenceType);
        }
    }
}
